<?php 
/*
 module:		会员管理控制器
 create_time:	2023-11-01 09:11:14
 author:		your name
 contact:		your email address
*/

namespace app\api\controller\Admin;
use think\exception\ValidateException;
use app\api\model\Admin\Member as MemberModel;
use app\api\controller\Common;
use think\facade\Db;

class Member extends Common {


	/**
	* @api {post} /Admin.Member/index 01、数据列表
	* @apiGroup Member
	* @apiVersion 1.0.0
	* @apiDescription  数据列表

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"

	* @apiParam (输入参数：) {string}			[status] 状态 [{"label":"开启","value":"1","label_color":"primary"},{"label":"关闭","value":"0","label_color":"danger"}]
	* @apiParam (输入参数：) {int}     			[limit] 每页数据条数（默认20）
	* @apiParam (输入参数：) {int}     			[page] 当前页码

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据
	* @apiParam (成功返回参数：) {string}     	array.data.list 返回数据列表
	* @apiParam (成功返回参数：) {string}     	array.data.count 返回数据总数
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" ","msg":"查询失败"}
 	*/
	public function index(): \think\response\Json 
	{
		$limit  = $this->request->post('limit', 20, 'intval');
		$page = $this->request->post('page', 1, 'intval');

		$where = [];
		$where['status'] = $this->request->post('status', null, 'serach_in');

		$field = 'member_id,username,mobile,email,name,gender,birthday,address';

		$res = MemberModel::where(formatWhere($where))->field($field)->order('member_id desc')->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();

		$data['status'] = 200;
		$data['data'] = $res;
		return json($data);
	}


	/**
	* @api {post} /Admin.Member/add 02、添加
	* @apiGroup Member
	* @apiVersion 1.0.0
	* @apiDescription  添加

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"

	* @apiParam (输入参数：) {varchar}			[username] 用户名 
	* @apiParam (输入参数：) {varchar}			[password] 密码 
	* @apiParam (输入参数：) {varchar}			[mobile] 手机号 
	* @apiParam (输入参数：) {varchar}			[email] 邮箱 
	* @apiParam (输入参数：) {varchar}			[name] 姓名 
	* @apiParam (输入参数：) {int}			[gender] 性别 [{"label":"男","value":"1","label_color":"primary"},{"label":"女","value":"2","label_color":"warning"}]
	* @apiParam (输入参数：) {datetime}			[birthday] 生日 
	* @apiParam (输入参数：) {text}			[address] 住址 
	* @apiParam (输入参数：) {int}			[sort_id] 排序 
	* @apiParam (输入参数：) {tinyint}			[status] 状态 [{"label":"开启","value":"1","label_color":"primary"},{"label":"关闭","value":"0","label_color":"danger"}]
	* @apiParam (输入参数：) {datetime}			[create_time] 创建时间 
	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" ","msg":"操作失败"}
 	*/
	public function add(): \think\response\Json 
	{
		$postField = 'username,password,mobile,email,name,gender,birthday,address,sort_id,status,create_time';
		$data = $this->request->only(explode(',',$postField),'post',null);
		$data['password'] = md5($data['password'].config('rds.password_secrect'));
		$data['create_time'] = date('Y-m-d H:i:s');
		$data['creater_id'] = $this->userInfo['user_id'];
		$data['creater_dept'] = $this->userInfo['dept_id'];
		$this->validate($data,\app\api\validate\Admin\Member::class);
		try{
			$res = MemberModel::create($data);
			if($res->member_id && empty($data['sort_id'])){
				 MemberModel::update(['sort_id'=>$res->member_id,'member_id'=>$res->member_id]);
			}
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res->member_id]);
	}


	/**
	* @api {post} /Admin.Member/update 03、修改
	* @apiGroup Member
	* @apiVersion 1.0.0
	* @apiDescription  修改

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"

	* @apiParam (输入参数：) {varchar}			[username] 用户名 
	* @apiParam (输入参数：) {varchar}			[mobile] 手机号 
	* @apiParam (输入参数：) {varchar}			[email] 邮箱 
	* @apiParam (输入参数：) {varchar}			[name] 姓名 
	* @apiParam (输入参数：) {int}			[gender] 性别 [{"label":"男","value":"1","label_color":"primary"},{"label":"女","value":"2","label_color":"warning"}]
	* @apiParam (输入参数：) {datetime}			[birthday] 生日 
	* @apiParam (输入参数：) {text}			[address] 住址 
	* @apiParam (输入参数：) {int}			[sort_id] 排序 
	* @apiParam (输入参数：) {tinyint}			[status] 状态 [{"label":"开启","value":"1","label_color":"primary"},{"label":"关闭","value":"0","label_color":"danger"}]
	* @apiParam (输入参数：) {datetime}			[update_time] 更新时间 
	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" ","msg":"操作失败"}
 	*/
	public function update(): \think\response\Json 
	{
		$postField = 'member_id,username,mobile,email,name,gender,birthday,address,sort_id,status,update_time';
		$data = $this->request->only(explode(',',$postField),'post',null);
		$data['update_time'] = date('Y-m-d H:i:s');
		$this->validate($data,\app\api\validate\Admin\Member::class);
		try{
			MemberModel::update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200]);
	}


	/**
	* @api {post} /Admin.Member/detail 04、查看详情
	* @apiGroup Member
	* @apiVersion 1.0.0
	* @apiDescription  查看详情

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"

	* @apiParam (输入参数：) {string}     		member_id 主键id
	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" ","msg":"操作失败"}
 	*/
	public function detail(): \think\response\Json 
	{
		$id =  $this->request->post('member_id', null, 'serach_in');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'member_id,username,mobile,email,name,gender,birthday,address';
		$res = MemberModel::field($field)->find($id);
		return json(['status'=>200,'data'=>$res]);
	}


	/**
	* @api {post} /Admin.Member/resetPwd 05、重置密码
	* @apiGroup Member
	* @apiVersion 1.0.0
	* @apiDescription  重置密码

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"

	* @apiParam (输入参数：) {string}     		member_id 主键id
	* @apiParam (输入参数：) {string}     		password 密码
	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" ","msg":"操作失败"}
 	*/
	public function resetPwd(): \think\response\Json 
	{
		$postField = 'member_id,password';
		$data = $this->request->only(explode(',',$postField),'post',null);
		if(empty($data['member_id'])) throw new ValidateException ('参数错误');
		if(empty($data['password'])) throw new ValidateException ('密码不能为空');

		$data['password'] = md5($data['password'].config('rds.password_secrect'));
		$res = MemberModel::update($data);
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/**
	* @api {post} /Admin.Member/login 06、用户登录
	* @apiGroup Member
	* @apiVersion 1.0.0
	* @apiDescription  用户登录
	* @apiParam (输入参数：) {string}     		username 用户名
	* @apiParam (输入参数：) {string}     		password 密码
	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" ","msg":"操作失败"}
 	*/
	public function login(): \think\response\Json 
	{
		$postField = 'username,password';
		$data = $this->request->only(explode(',',$postField),'post',null);
		if(empty($data['username'])) throw new ValidateException ('用户名不能为空');
		if(empty($data['password'])) throw new ValidateException ('密码不能为空');

		$fields = 'member_id,username,mobile,email,name,gender,birthday,address';

		$where['username'] = $data['username'];
		$where['password'] = md5($data['password'].config('rds.password_secrect'));

		$res = MemberModel::field($fields)->where($where)->find();
		if(!$res){
			throw new ValidateException ('账号或密码错误');
		}
		return json(['status'=>200,'data'=>$res,'token'=>$this->setToken(json_encode($res))]);
	}


	/**
	* @api {post} /Admin.Member/sendSms 07、发送验证码
	* @apiGroup Member
	* @apiVersion 1.0.0
	* @apiDescription  发送验证码
	* @apiParam (输入参数：) {string}     		captcha 图片验证码
	* @apiParam (输入参数：) {string}     		key 验证码key
	* @apiParam (输入参数：) {string}     		mobile 手机号
	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" ","msg":"操作失败"}
 	*/
	public function sendSms(): \think\response\Json 
	{
		$mobile = $this->request->post('mobile');
		if(empty($mobile)) throw new ValidateException ('手机号不能为空');
		if(!preg_match('/^1[3456789]\d{9}$/',$mobile)) throw new ValidateException ('手机号格式错误');

		$data['mobile']	= $mobile;	//发送手机号
		$data['code']	= sprintf('%06d', rand(0,999999));		//验证码
		try{
			// 仅测试使用
			\think\facade\Log::info('仅测试的验证码');
			\think\facade\Log::info($data);
		}catch(\Exception $e){
			abort(config('rds.error_log_code'),$e->getMessage());
		}
		$key = md5(time().$data['mobile']);
		cache($key,['mobile'=>$data['mobile'],'code'=>$data['code']],300);
		return json(['status'=>200,'msg'=>'发送成功','key'=>$key]);
	}


	/**
	* @api {post} /Admin.Member/login_mobile 08、用户登录
	* @apiGroup Member
	* @apiVersion 1.0.0
	* @apiDescription  用户登录
	* @apiParam (输入参数：) {string}     		mobile 短信验证手机号
	* @apiParam (输入参数：) {string}     		verify_id 短信验证ID
	* @apiParam (输入参数：) {string}     		verify 短信验证码
	* @apiParam (输入参数：) {string}     		mobile 手机号
	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码  
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"","data":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" ","msg":"操作失败"}
 	*/
	public function login_mobile(): \think\response\Json 
	{
		$postField = 'mobile';
		$data = $this->request->only(explode(',',$postField),'post',null);
		if(empty($data['mobile'])) throw new ValidateException ('手机号不能为空');

		$fields = 'member_id,username,mobile,email,name,gender,birthday,address';

		$where['mobile'] = $data['mobile'];

		$res = MemberModel::field($fields)->where($where)->find();
		if(!$res){
			throw new ValidateException ('登录失败，请检查手机号');
		}
		return json(['status'=>200,'data'=>$res,'token'=>$this->setToken(json_encode($res))]);
	}



	

}

